package uk.co.kevinlockwood.kol;

import uk.co.kevinlockwood.kol.droid.DroidHelpers;
import uk.co.kevinlockwood.kol.pages.KoLPageBuilder;
import uk.co.kevinlockwood.kol.pages.LoginPage;
import uk.co.kevinlockwood.kol.pages.LoginPage.LoginResult;

/**
 * Helper classes to talk to Kingdom of Loathing
 * @author kevin
 */
public class KoLHelpers {
	/**
	 * Logs a user into KoL and returns the Session ID, or null if login fails.
	 * @param user
	 * @param password
	 * @return sessionid string
	 */
	public static LoginResult getSessionId(KoLPageBuilder builder, String user, String passhash) {
		LoginResult r = null;
		LoginPage l = new LoginPage(user, null, null);
		builder.buildPage(l);	// this sets the challenge string
		String challenge = l.getChallenge();
		DroidHelpers.debug("got login challenge of [",challenge,"]");
		String response = JavaHelpers.md5sum(passhash+":"+challenge);
		l.setResponse(response);
		builder.buildPage(l);	// this gets the session id
		r = l.getResult();
		return r;
	}
}
